#!/usr/bin/env python
# -*- coding: UTF-8 -*-
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

from alert.common import conf


CONF = conf.CONF


try:
    db_host = CONF.get("db", "db_host")
    db_port = CONF.get("db", "db_port")
    db_user = CONF.get("db", "db_user")
    db_pass = CONF.get("db", "db_pass")
    db_name = CONF.get("db", "db_name")

    DB_CONNECT_STRING = ('mysql+mysqldb://%s:%s@%s:%s/%s?charset=utf8' %
                         (db_user, db_pass, db_host, db_port, db_name))

    engine = create_engine(DB_CONNECT_STRING, echo=False)
    DBSession = sessionmaker(bind=engine)
    session = DBSession()
except Exception:
    print "init DB session failed."
    raise


def get_session():
    return DBSession()


def close_session(se):
    se.close()
